<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
use api\wxapp\model\ShopGoods;
use api\wxapp\model\Goods;
use api\wxapp\model\Shop;
use api\wxapp\model\Score;
use api\wxapp\model\User;
use api\wxapp\model\Order;
use api\wxapp\model\OrderList;
use api\wxapp\model\ScoreGoods;
class ScoreController extends AdminBaseController
{
    private $shop_id;
    private $where = array();
    public function initialize()
    {
        parent::initialize();
        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
            //说明是店铺登陆
            $find = db::name('app_shop')->where('admin_id',$sessionAdminId)->find();
            $this->shop_id = $find['id'];
            $this->where[] = ['t1.shop_id','eq',$find['id']];
        }
    }
    public function index()
    {
        $params = $this->param();
        //$this->where[] = ['t1.is_scoreshop','eq',2];
        if(isset($params['name'])){
            $this->where[] = ['t2.name','like','%'.$params['name'].'%'];
        }
        $list = ScoreGoods::alias('t1')
            ->join('goods t2','t2.id = t1.goods_id')
            ->join('app_shop t3','t3.id = t1.shop_id')
            ->field('t1.*,t2.name,t3.name as shopname,t2.img')
            ->where($this->where)
            ->paginate(15);
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    public function add()
    {
        $this->shop_id = 1;
        if(!$this->shop_id){
            $this->error('此权限对店铺开放!!!');
        }
        if($_POST){
            if(!$_POST['score']){
                $this->error('所需积分不能为空');
            }
            $_POST['shop_id'] = $this->shop_id;
            $_POST['addtime'] = date('Y-m-d H:i:s',time());
            $res = ScoreGoods::insert($_POST);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        //查询出当前所有的积分商品的id
        $idarr = ScoreGoods::where('shop_id',$this->shop_id)->column('goods_id');
        $where[] = ['shop_id','eq',$this->shop_id];
        $where[] = ['goods_id','NOTIN',$idarr];
        $goods = ShopGoods::where($where)->field('goods_id')->all()
            ->each(function($item, $key){
            $item['name'] = Goods::where('id',$item['goods_id'])->value('name');
        });

        $this->assign('goods',$goods);
        $this->assign('shop_id',$this->shop_id);
        return $this->fetch();
    }

    public function edit()
    {
        $params = $this->param();
        if($_POST){
            $res = ScoreGoods::update($_POST);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $info = ScoreGoods::get($params['id']);
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function delete()
    {
        $params = $this->param();
        $res = ScoreGoods::destroy($params['id']);
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
    public function record(Score $score)
    {
        $params = $this->param();
        $where = array();
        if($this->shop_id){
            $idarr = User::where('shop_id',$this->shop_id)->column('id');
            $where[] = ['t1.uid','in',$idarr];
        }
        if(isset($params['nickname'])){
            $where[] = ['t2.nickname','like','%'.$params['nickname'].'%'];
        }
        if(isset($params['phone'])){
            $where[] = ['t2.phone','like','%'.$params['phone'].'%'];
        }
        $query = Score::alias('t1');
        $query = $score->leftuser($query);
        $query = $score->returnfield($query);
        $list = $query->where($where)->paginate(15);


        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    public function list(Order $order)
    {
        $params = $this->param();
        $where = array();
        if($this->shop_id){
            $where[] = ['t1.shop_id','eq',$this->shop_id];
        }
        if(isset($params['nickname'])){
            $where[] = ['t2.nickname','like','%'.$params['nickname'].'%'];
        }
        if(isset($params['phone'])){
            $where[] = ['t2.phone','like','%'.$params['phone'].'%'];
        }
        $where[] = ['t1.score','gt',0];
        $query = Order::alias('t1');
        $query = $order->leftuser($query);
        $query = $order->leftshop($query);
        $query = $order->returnfield($query);
        $query = $order->orderId($query);
        $list = $query->where($where)->paginate(15)
            ->each(function($item,$key){
            $item['goods_id'] = OrderList::where('order_id',$item['id'])->value('goods_id');
            $item['goodsname'] = Goods::where('id',$item['goods_id'])->value('name');
        });
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
        return $this->fetch();
    }
}
